<?php
class MemberAction extends CommonAction{
      /*+------------------------------
        |     添加用户
        +------------------------------
       */
       public function addMember(){
              if($_POST['create']){
                 //echo "<pre>";
                 //print_r($_POST);
                $member = D("sysuser");
                foreach($_POST as $key => $val){
                       if($key != "__hash__" && $key != "create"){
                               if($key == "sysuser_password"){
                                   $data[$key] = MD5($_POST[$key]);
                               }else{
                                   $data[$key] = $_POST[$key];
                               }
                       }
                }
                $data['create_user'] = $_SESSION[C("USER_AUTH_KEY")];
                $data['create_time'] = toDate(time());
                $data['userType']    = "0";
                $add    = $member ->add($data);
                if($add){
                       $this->success("添加用户成功！");
                }else{
                       $this->error("添加用户失败！");
                }

              }
              $this->display();
       }

      /*+-----------------------------
        |     管理用户
        +-----------------------------
       */
       public function manageMember(){
              $order = "create_time desc";
              $limit = $this->pageOperate("sysuser","15");
              $this->getGroupInfo($this->query($order,$limit['firstRow'].",".$limit['listRows']));
              $this->display();
       }
	   
	   /*+---------------------------
	     |    管理用户的功能
		 +---------------------------
		 */
	    public function manageFunction(){
			    $fun       = D("sysuser");
				$function  = $fun ->where("id = {$_GET['Uid']}") ->find();
				$count     = substr_count($function['function'],",");
				
				if($count >0 ){
				      	$in_array  = explode(",",$function['function']);
				}else{
				        $in_array  = array("0"=>$function['function']);	
			    }
                
			    $param['after']  = array(
									  'content'=>'&nbsp;<input type="checkbox" CK name="id[]" value="PK"/>&nbsp;&nbsp;',
									  'explode'=>array(
													   '0'=>array(
															      'PK'  =>'CK',
																  'swap'=>'checked',
															 ),
													   '1'=>array(
																  'PK'  =>'PK',
																  'field'=>'id',
															 ),
													   
													   ),
									  'in_array' => $in_array,
									  ); 
				$param['link']   = "0";
		        $menu = $this ->get_menu(136,$param);
				$this->assign("manageFunction",$menu);
				$this->assign("Uid",$_GET['Uid']);
				if($_POST['save']){
					      foreach($_POST['id'] as $key => $val ){
							         $funct .= $val.",";
						  } 
						  
						  $funct            = rtrim($funct,",");
						  //echo $function;
						  //exit;
						  $data['id']          = $_POST['Uid'];
						  $data['function']    = $funct;
						  $data['update_time'] = toDate(time());
                          $update              = $fun ->save($data);
                          $this                ->editWebsiteGroupFunction($data);//同步更新站点管理员的功能信息
						  if($update){
							   $this->success("用户功能修改成功！");
						  }else{
							   $this->error("用户功能修改失败！");   
						  }
				}
				$this->display();
	    }

      /*+----------------------------
        |      编辑用户信息
        +----------------------------
       */
       public function editUserInfo(){
          
           $editUsers = D("sysuser");
           if($_POST['save']){
           	       //获取站点数据库名
           		   $databaseArray = $_POST['database'];
           		   //------------------------------------
           		   unset($_POST['database']);
                   foreach($_POST as $key => $val){
                           if(is_array($_POST[$key])){
                               foreach($val as $k => $v){
                               			$v = trim($v);
                                        if($key == "sysuser_password"){
                                        	if($v)
                                        	{
                                            	$data[$k][$key] = MD5($v);
                                        	}
                                        }else if($key=='sysuser_name' && !$v){
                                        		
                                        	 $this->error("系统用户账号不能为空，请返回修改！");
                                        	 exit;
                                        }
                                        else{
                                              $data[$k][$key] = $v;
                                        }
                               }
                           }
                   }
				   //$count = 0;
                   foreach($data as $key => $val){
                                $edit += $editUsers ->save($val);
                                //处理站点用户信息
                                if($databaseArray[$key])
                                {
                                	$editUsers->query('use '.$databaseArray[$key]);
	                                $editUsers = D("sysuser");
	                                $editUsers ->where('1=1')->save($val);
                                }
                                //$count++;
                   } 
                   if($edit){
                         $this->assign("jumpUrl","manageMember");
                         $this->success("成功的修改了 {$edit} 个用户信息！");
                   }else{
                         $this->error("修改用户信息失败！");
                   }
           }else{
	            if($_GET['Gid']){
	               $where = "id = {$_GET['Gid']}";
	            }
	           	if($_POST['edit']){
	               foreach($_POST['id'] as $key => $val){
	                     $in .= $val.",";
	               }
	               $in = rtrim($in,",");
	               $where = "id in ({$in})"; 
	           }
	           $where .= " and user_type = '0'";
	           $usersInfo = $editUsers -> where($where)->select();
	           $this->assign("usersInfo",$usersInfo);
	           $this->display();
           }
       }
      /*+----------------------------
        |     用户审核
        +----------------------------
       */
       public function verifyMember(){
           
              $order = "create_time desc";
              $limit = $this->pageOperate("sysuser","15");
              $this->getGroupInfo($this->query($order,$limit['firstRow'].",".$limit['listRows']));
              if($_GET['Uid']){
                   $where = "id = {$_GET['Uid']}";
              }
              $sysuser = D("sysuser");
              if($_POST['check']){
                  
                  if($_POST['status']){
                      foreach($_POST['status'] as $key => $val){
                               if($val == '1'){
                                     $_POST['status'][$key] = "0";
                               }else{
                                     $_POST['status'][$key] = "1";
                               }
                        }
                
                  }
                  foreach($_POST as $key => $val){
                      foreach($val as $k => $v){
                               $data[$k][$key] = $v;
                      }
                          
                  }
              }
			  
              if($_GET['Uid'] && isset($_GET['sta'])){
				    $data[0]['id']  = $_GET['Uid']; 
                    if($_GET['sta'] == '1'){
                         $data[0]['status'] = '0';
                    }
					if($_GET['sta'] == '0'){
                         $data[0]['status'] = '1';
                    }
              }
			  
              if($data){
                      foreach($data as $key => $val){
                         $update +=$sysuser ->save($val);
              }

              if($update){
                          $this->success("成功的审核了 {$update} 个用户！");
              }else{
                          $this->error("用户审核失败！");
              }
              }

              $this->display();
       }
	   
	   //多表查询语句、
    public function query($order,$limit,$query){
	    if($query){
		       $query = $query;
		}else{
              $query = "select a.*,b.sysuser_nikename create_user from douhao_sysuser a,douhao_sysuser b where a.create_user = b.id and a.user_type = '0'";
	    }
		
        if($order){
             $query .= " order by a.".$order;

        }
        if($limit){
            $query .= " limit ".$limit;
        }
        return $query;

    }

    //修改站点的用户功能信息
    public function editWebsiteGroupFunction($data){
        $user     = D("sysuser");
        $webGroup = $user ->field("database") -> where("id = {$data['id']}")->find();
        $func = new Model();
        $sql  = "update {$webGroup['database']}.douhao_group set group_function = '{$data['function']}' where id = 1;";
        $func ->query($sql);
    }
}
?>
